package cn.jbolt.xd;

import cn.jbolt.common.model.xd.XdBillAccount;
import cn.jbolt.common.model.xd.XdBillFlows;
import cn.jbolt.common.model.xd.XdBillZixiongWeixin6216;

import java.math.BigDecimal;
import java.util.List;

public class XdBillFlowsUtils {

    public static void main(String[] args) {
        XdDBUtils.contect();
//        step1();
        step3();
    }

    private static void step1(){
        List<XdBillZixiongWeixin6216> list = new XdBillZixiongWeixin6216().dao()
                .find("SELECT * FROM xd_bill_zixiong_weixin6216 WHERE is_operating_income=0");
        for (int i = 0; i < list.size(); i++) {
            XdBillZixiongWeixin6216 item = list.get(i);
            XdBillFlows xdBillFlows = new XdBillFlows().dao().findFirst("select * from xd_bill_flows where source_id=?",item.getId());
            if (xdBillFlows == null) {
                xdBillFlows = new XdBillFlows();
            }
            xdBillFlows.setTradeTime(item.getTradeTime());
            xdBillFlows.setType("转账");
            xdBillFlows.setAmount(item.getAmount());
            xdBillFlows.setRemark(item.getProduct());
            xdBillFlows.setState(0);
            xdBillFlows.setBillAlias(item.getBillAlias());
            xdBillFlows.setSourceId(item.getId());
            if(item.getTradeMark().equals("支出")){
                xdBillFlows.setOutAccount(item.getPayChannal());
                xdBillFlows.setInAccount(item.getCounterparty());
            }else if(item.getTradeMark().equals("/")){
                if (item.getTradeType().equals("零钱充值")){
                    xdBillFlows.setOutAccount(item.getPayChannal());
                    xdBillFlows.setInAccount("魏梓雄_微信6216");
                }else if (item.getTradeType().equals("零钱提现")){
                    xdBillFlows.setOutAccount("魏梓雄_微信6216");
                    xdBillFlows.setInAccount(item.getCounterparty());
                }else if (item.getTradeType().equals("转入零钱通-来自零钱")){
                    xdBillFlows.setOutAccount("魏梓雄_微信6216");
                    xdBillFlows.setInAccount("魏梓雄_零钱通6216");
                }else if (item.getTradeType().equals("信用卡还款")){
                    if (item.getPayChannal().equals("零钱通")){
                        xdBillFlows.setOutAccount("魏梓雄_零钱通6216");
                        xdBillFlows.setInAccount(item.getCounterparty());
                    }else{
                        xdBillFlows.setOutAccount("魏梓雄_微信6216");
                        xdBillFlows.setInAccount(item.getCounterparty());
                    }
                }else{
                    xdBillFlows.setOutAccount(item.getPayChannal());
                    xdBillFlows.setInAccount(item.getCounterparty());
                }
            } else if(item.getTradeMark().equals("收入")){
                if (item.getPayChannal().equals("/")
                        || item.getPayChannal().equals("零钱")){
                    xdBillFlows.setOutAccount(item.getCounterparty());
                    xdBillFlows.setInAccount("魏梓雄_微信6216");
                }else if (item.getPayChannal().equals("零钱通")){
                    xdBillFlows.setOutAccount(item.getCounterparty());
                    xdBillFlows.setInAccount("魏梓雄_零钱通6216");
                }else{
                    xdBillFlows.setOutAccount(item.getCounterparty());
                    xdBillFlows.setInAccount(item.getPayChannal());
                }

            }

            xdBillFlows.setOutAccount(xdBillFlows.getOutAccount().replace("\t|\r|\n", "").replace("\"", "").trim());
            xdBillFlows.setInAccount(xdBillFlows.getInAccount().replace("\t|\r|\n", "").replace("\"", "").trim());

            XdBillAccount inAccount = new XdBillAccount().dao().findFirst("select * from xd_bill_account where alias=?",xdBillFlows.getInAccount());
            if (inAccount != null) {
                xdBillFlows.setInAccount(inAccount.getName());
            }
            XdBillAccount outAccount = new XdBillAccount().dao().findFirst("select * from xd_bill_account where alias=?",xdBillFlows.getOutAccount());
            if (outAccount != null) {
                xdBillFlows.setOutAccount(outAccount.getName());
            }

            System.out.println(xdBillFlows);
            if (xdBillFlows.getId() == null) {
                xdBillFlows.save();
            }else{
                xdBillFlows.update();
            }
        }
    }

    private static void step2(){
        List<XdBillFlows> flowsList = new XdBillFlows().findAll();
        for (int i = 0; i < flowsList.size(); i++) {
            XdBillFlows item = flowsList.get(i);
            XdBillAccount outAcount = new XdBillAccount().dao()
                    .findFirst("SELECT * from xd_bill_account WHERE  alias=?",item.getOutAccount());
//            System.out.println(item);
//            System.out.println(outAcount);
            if(outAcount!=null){
                item.setOutAccount(outAcount.getName());
            }
            XdBillAccount inAcount = new XdBillAccount().dao()
                    .findFirst("SELECT * from xd_bill_account WHERE  alias=?",item.getInAccount());
//            System.out.println(inAcount);
            if(inAcount!=null){
                item.setInAccount(inAcount.getName());
            }
            item.update();
        }
    }

    private static void step3(){
        List<XdBillFlows> flowsList = new XdBillFlows().dao().find("select * from xd_bill_flows order by trade_time asc");
        BigDecimal last = new BigDecimal(599.43);
        for (int i = 0; i < flowsList.size(); i++) {
            XdBillFlows item = flowsList.get(i);
            if (item.getOutAccount().equals("魏梓雄_微信6216")) {
                BigDecimal balance = last.subtract(item.getAmount());
                item.setBalance(balance);
                item.update();
                last = item.getBalance();
            }else if(item.getInAccount().equals("魏梓雄_微信6216")){
                BigDecimal balance = last.add(item.getAmount());
                item.setBalance(balance);
                item.update();
                last = item.getBalance();
            }
            System.out.println(item);

        }
    }
}
